package pers.cnzdy.tutorial.Chapter7_Data.Section4_ContentProvider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

import pers.cnzdy.tutorial.Chapter7_Data.Section3_SQLite.DBQuizHelper;


public class QuizProvider extends ContentProvider {
    private static final int QUIZ = 0;
    private static final int COURSE = 1;

    private static final String AUTHORITY =
            "pers.cnzdy.tutorial.quiz.provider";

    //1 想使用内容提供者 必须定义 匹配规则
    // code:定义的匹配规则 如果 匹配不上  有一个返回码  -1
    static UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);

    private DBQuizHelper helper;
    private String table = "quiz";

    static{
        //开始添加匹配规则
        /**
         * authority   主机名  通过主机名来访问共享的数据
         * path   pers.cnzdy.tutorial.quiz.provider
         * code 匹配码
         */
        matcher.addURI(AUTHORITY, "quiz", QUIZ);
        matcher.addURI(AUTHORITY, "course", COURSE);
    }

    @Override
    public boolean onCreate() {
        helper = new DBQuizHelper(getContext(), "Exam.db", null, 2);
        return true;
    }

    @Override
    public String getType(Uri uri) {
//        return "vnd.android.cursor.dir/" +
//                "vnd.pers.cnzdy.tutorial.quiz.provider.quiz";
        return null;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder) {
        int match = matcher.match(uri);

        if (match == QUIZ) {
            SQLiteDatabase db = helper.getReadableDatabase();
            Cursor cursor = db.query(table, projection, selection,
                    selectionArgs, null, null, sortOrder);
            getContext().getContentResolver().notifyChange(uri, null);
            return cursor;
        }else{
            throw new IllegalArgumentException("路径匹配失败");
        }
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        int match = matcher.match(uri);

        if (match == QUIZ) {
            SQLiteDatabase db = helper.getReadableDatabase();
            long rowId = db.insert(table, null, values);

            if (rowId > 0) {
                getContext().getContentResolver().notifyChange(uri, null);
            }
            Uri uriResult = Uri.parse("content://"+ AUTHORITY + "/quiz/" + rowId);
            return uriResult;
        }else{
            throw new IllegalArgumentException("路径匹配失败");
        }
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        int match = matcher.match(uri);

        if (match == QUIZ) {
            SQLiteDatabase db = helper.getReadableDatabase();
            int delete = db.delete(table, selection, selectionArgs);
            if (delete > 0) {
                getContext().getContentResolver().notifyChange(uri, null);
            }
            return delete;
        }else {
            throw new IllegalArgumentException("路径匹配失败");
        }
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
                      String[] selectionArgs) {
        int match = matcher.match(uri);
        if (match == QUIZ) {
            SQLiteDatabase db = helper.getReadableDatabase();
            int update = db.update(table, values, selection, selectionArgs);
            if (update > 0) {
                getContext().getContentResolver().notifyChange(uri, null);
            }
            return update;
        }else {
            throw new IllegalArgumentException("路径匹配失败");
        }
    }
}
